/**  
 * @Title: TaskController.java
 * @Package com.xzit.tmms.controller.task
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 下午4:09:14
 * @version V1.0  
 */
package com.xzit.tmms.controller.task;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.xzit.tmms.bean.task.Meterial;
import com.xzit.tmms.bean.task.Task;
import com.xzit.tmms.bean.unit.Teacher;
import com.xzit.tmms.common.base.BaseController;
import com.xzit.tmms.common.context.TmmsContext;
import com.xzit.tmms.common.exception.ServiceException;
import com.xzit.tmms.common.exception.UtilException;
import com.xzit.tmms.common.page.Page;
import com.xzit.tmms.common.util.CommonUtil;
import com.xzit.tmms.common.util.FileUtil;
import com.xzit.tmms.service.course.CourseService;
import com.xzit.tmms.service.task.MeterialService;
import com.xzit.tmms.service.task.TaskService;
import com.xzit.tmms.service.teacher.TeacherService;
import com.xzit.tmms.service.unit.UnitService;

/**
 * @ClassName: TaskController
 * @Description: TODO
 * @author 董亮亮
 * @date 2016年6月27日 下午4:09:14
 */
@Controller
@RequestMapping("/task")
public class TaskController extends BaseController {

	@Resource
	private UnitService unitService;

	@Resource
	private CourseService courseService;

	@Resource
	private TaskService taskService;

	@Resource
	private MeterialService meterialService;

	@Resource
	private TeacherService teacherService;

	/**
	 * @Title: index
	 * @Description: TODO 加载任务索引界面
	 */
	@RequestMapping("")
	public String index() {
		return "task/task-index";
	}

	/**
	 * @Title: queryTask
	 * @Description: TODO 条件查询教学任务计划
	 */
	@RequestMapping("/queryTask")
	public String queryTask(Model model, Task task) throws Exception {
		Page<Map<String, Object>> page = new Page<>(getPageIndex(), getPageSize());
		model.addAttribute("page", taskService.queryTask(page, task));
		model.addAttribute("yearList", TmmsContext.yearList);
		model.addAttribute("clazzList", unitService.getClassList());
		model.addAttribute("courseList", courseService.getCourseList());
		model.addAttribute("task", task);
		return "task/task-query";
	}

	/**
	 * @Title: addTask
	 * @Description: TODO 添加任务
	 */
	@RequestMapping("/addTask")
	public String addTask(Model model) throws ServiceException {
		model.addAttribute("clazzList", unitService.getClassList());
		model.addAttribute("courseList", courseService.getCourseList());
		model.addAttribute("weekList", TmmsContext.weekList);
		model.addAttribute("yearList", TmmsContext.yearList);
		return "task/task-add";
	}

	/**
	 * @Title: addTask
	 * @Description: TODO 添加任务
	 */
	@RequestMapping("/addTaskSubmit")
	public String addTaskSubmit(Task task, Integer[] weeks) throws UtilException, ServiceException {
		task.setProgress(CommonUtil.makeUpIds(weeks));
		taskService.addTask(task);
		return "redirect:/task.action";
	}

	/**
	 * @Title: deleteTask
	 * @Description: TODO 删除任务
	 */
	@RequestMapping("/deleteTask")
	public String deleteTask(Integer taskId) throws ServiceException {
		taskService.deleteTask(taskId);
		return "forward:queryTask.action";
	}

	@RequestMapping("/updateTask")
	public String updateTask(Model model, Integer taskId) throws ServiceException {
		// 查询任务
		Task task = taskService.geTaskById(taskId);
		// 添加数据到model
		model.addAttribute("task", task);
		// 加载相应的属性列表
		model.addAttribute("weeks", CommonUtil.splitIds(task.getProgress()));
		model.addAttribute("clazzList", unitService.getClassList());
		model.addAttribute("courseList", courseService.getCourseList());
		model.addAttribute("weekList", TmmsContext.weekList);
		model.addAttribute("yearList", TmmsContext.yearList);
		return "task/task-update";
	}

	/**
	 * @Title: updateTaskSubmit @Description: TODO 更新任务信息 @throws
	 */
	@RequestMapping("/updateTaskSubmit")
	public String updateTaskSubmit(Task task, Integer[] weeks) throws UtilException, ServiceException {
		task.setProgress(CommonUtil.makeUpIds(weeks));
		taskService.updateTask(task);
		return "redirect:/task.action";
	}

	/**
	 * @Title: detailTask
	 * @Description: TODO 加载任务详情
	 */
	@RequestMapping("/detailTask")
	public String detailTask(Model model, Integer taskId) throws ServiceException {
		// task详情
		Map<String, Object> task = taskService.queryTaskDetail(taskId);
		// 查询任课教师
		if (task.get("teacherId") != null) {
			Integer teacherId = Integer.parseInt(task.get("teacherId").toString());
			Teacher teacher = teacherService.getTeacherById(teacherId);
			model.addAttribute("teacher", teacher);
		}
		// 材料归档记录
		List<Map<String, Object>> meteList = meterialService.queryMetesByTask(taskId);
		// 数据返回
		model.addAttribute("task", task);
		model.addAttribute("meteList", meteList);
		model.addAttribute("weeks", CommonUtil.splitIds(task.get("progress").toString()));
		model.addAttribute("teachers", teacherService.getTeacherList());
		return "task/task-detail";
	}

	/**
	 * @Title: uploadMete
	 * @Description: TODO 上传教学材料
	 */
	@RequestMapping("/uploadMete")
	public String uploadMete(RedirectAttributes attr, Meterial meterial, MultipartFile meteFile)
			throws UtilException, ServiceException {
		if (meteFile != null) {
			meterial.setMetePath(FileUtil.saveFile(meteFile));
		}
		meterialService.updateMete(meterial);
		attr.addAttribute("taskId", meterial.getTaskId());
		return "redirect:/task/detailTask.action";
	}

	/**
	 * @Title: selectTeacher
	 * @Description: TODO 选择教师
	 */
	@RequestMapping("/selectTeacher")
	public String selectTeacher(RedirectAttributes attr, Task task) throws ServiceException {
		taskService.updateTask(task);
		attr.addAttribute("taskId", task.getTaskId());
		return "redirect:/task/detailTask.action";
	}
}
